package com.team.order.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.team.order.mapper.AccountFlowMapper;
import com.team.order.domain.AccountFlow;
import com.team.order.service.AccountFlowService;

/**
 * 商户收支明细Service业务层处理
 * 
 * @author 梁国瑞
 * @date 2025-07-10
 */
@Service
public class AccountFlowServiceImpl implements AccountFlowService {
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    /**
     * 查询商户收支明细
     * 
     * @param id 商户收支明细主键
     * @return 商户收支明细
     */
    @Override
    public AccountFlow selectAccountFlowById(Long id) {
        return accountFlowMapper.selectAccountFlowById(id);
    }

    /**
     * 查询商户收支明细列表
     * 
     * @param accountFlow 商户收支明细
     * @return 商户收支明细
     */
    @Override
    public List<AccountFlow> selectAccountFlowList(AccountFlow accountFlow) {
        return accountFlowMapper.selectAccountFlowList(accountFlow);
    }

    /**
     * 新增商户收支明细
     * 
     * @param accountFlow 商户收支明细
     * @return 结果
     */
    @Override
    public int insertAccountFlow(AccountFlow accountFlow) {
        return accountFlowMapper.insertAccountFlow(accountFlow);
    }

    /**
     * 修改商户收支明细
     * 
     * @param accountFlow 商户收支明细
     * @return 结果
     */
    @Override
    public int updateAccountFlow(AccountFlow accountFlow) {
        return accountFlowMapper.updateAccountFlow(accountFlow);
    }

    /**
     * 批量删除商户收支明细
     * 
     * @param ids 需要删除的商户收支明细主键
     * @return 结果
     */
    @Override
    public int deleteAccountFlowByIds(Long[] ids) {
        return accountFlowMapper.deleteAccountFlowByIds(ids);
    }

    /**
     * 删除商户收支明细信息
     * 
     * @param id 商户收支明细主键
     * @return 结果
     */
    @Override
    public int deleteAccountFlowById(Long id) {
        return accountFlowMapper.deleteAccountFlowById(id);
    }
    
    /**
     * 创建订单收入流水
     * 
     * @param orderId 订单ID
     * @param orderNo 订单号
     * @param merchantId 商户ID
     * @param amount 金额
     * @param payType 支付方式
     * @param thirdPartyOrderNo 第三方订单号
     * @return 结果
     */
    @Override
    public int createOrderIncomeFlow(Long orderId, String orderNo, Long merchantId, BigDecimal amount, Long payType, String thirdPartyOrderNo) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setMerchantId(merchantId);
        accountFlow.setOrderId(orderId);
        accountFlow.setType("income"); // 收入
        accountFlow.setAmount(amount);
        accountFlow.setSource("order_payment"); // 订单支付
        accountFlow.setRemark(String.format("订单支付收入，订单号：%s，支付方式：%s，第三方订单号：%s", 
            orderNo, getPayTypeName(payType), thirdPartyOrderNo));
        accountFlow.setFlowTime(LocalDateTime.now());
        accountFlow.setCreateTime(new Date());
        accountFlow.setUpdateTime(new Date());
        
        return accountFlowMapper.insertAccountFlow(accountFlow);
    }
    
    /**
     * 创建订单退款流水
     * 
     * @param orderId 订单ID
     * @param orderNo 订单号
     * @param merchantId 商户ID
     * @param amount 退款金额
     * @param refundReason 退款原因
     * @return 结果
     */
    @Override
    public int createOrderRefundFlow(Long orderId, String orderNo, Long merchantId, BigDecimal amount, String refundReason) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setMerchantId(merchantId);
        accountFlow.setOrderId(orderId);
        accountFlow.setType("refund"); // 退款
        accountFlow.setAmount(amount);
        accountFlow.setSource("order_refund"); // 订单退款
        accountFlow.setRemark(String.format("订单退款，订单号：%s，退款原因：%s", orderNo, refundReason));
        accountFlow.setFlowTime(LocalDateTime.now());
        accountFlow.setCreateTime(new Date());
        accountFlow.setUpdateTime(new Date());
        
        return accountFlowMapper.insertAccountFlow(accountFlow);
    }
    
    /**
     * 获取支付方式名称
     * 
     * @param payType 支付方式
     * @return 支付方式名称
     */
    private String getPayTypeName(Long payType) {
        if (payType == null) {
            return "未知";
        }
        switch (payType.intValue()) {
            case 0:
                return "微信支付";
            case 1:
                return "支付宝";
            case 2:
                return "银行卡";
            default:
                return "未知";
        }
    }
} 